package
{
	import flash.display.MovieClip;
	
	public class Unit
	{
		public var movieClip:MovieClip;
		public var x:Number,y:Number,xMove:Number,yMove:Number,speed:Number,xSpeed:Number,ySpeed:Number;
		public var count:uint;
		public var state:String = "stand";
		public var attack:uint,defense:uint,minDamage:uint,maxDamage:uint,HP:uint,currentHP:uint;
		public function Unit()
		{
			
		}
		public function BeginMove(x:int,y:int):void
		{
			this.xMove = x;
			this.yMove = y;
			state = "move";
			var s:Number = Math.sqrt(Math.pow(this.xMove-this.x,2)+Math.pow(this.yMove-this.y,2));
			var time:Number = s/speed;
			xSpeed = (this.xMove-this.x)/time;
			ySpeed = (this.yMove-this.y)/time;
			movieClip.ReceiveMessage("move",null);
		}
		public function Update(dt:uint):void
		{
			switch(state)
			{
				case "stand":
					break;
				case "move":
					
					x += xSpeed*dt;
					y += ySpeed*dt;
					if((xMove - x)*xSpeed < 0 || (yMove - y)*ySpeed < 0)
					{
						x = xMove;
						y = yMove;
						state = "stand";
						movieClip.ReceiveMessage("stand",null);
					}
					break;
			}
		}
		public function Damaged(damage:int):void
		{
			var totalHP:int = count*HP - HP + currentHP - damage;
			count = (uint)(totalHP/HP);
			currentHP = totalHP % HP;
			if(currentHP == 0)
			{
				count++;
				currentHP = HP;
			}
		}
	}
}